﻿'This file is part of Alpha.
'
'   Alpha is free software: you can redistribute it and/or modify
'   it under the terms of the GNU Lesser General Public License as published by
'   the Free Software Foundation, either version 3 of the License, or
'   (at your option) any later version.
'
'   Alpha is distributed in the hope that it will be useful,
'   but WITHOUT ANY WARRANTY; without even the implied warranty of
'   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
'   GNU Lesser General Public License for more details.
'
'   You should have received a copy of the GNU Lesser General Public License
'   along with Alpha.  If not, see <http://www.gnu.org/licenses/>.

Namespace Controllers
    Public Class Wmp
        Inherits Controller

#Region " Windows API Functions "

        Shared Sub SendCommand(ByVal command As Integer)
            'It may seem pointless to fetch WMP's handle every time we send a command, but we have no way to check if WMP has closed since we last sent a command, so this is a necessary check
            Dim hWnd As IntPtr = NativeMethods.FindWindow("WMPlayerApp", "Windows Media Player")
            If hWnd.ToInt32 = 0 Then
                Throw New PlayerNotRunningException
            End If
            Dim wParam As IntPtr = New IntPtr(command)
            '273 is the integer value of WM_COMMAND, found in win32.h
            NativeMethods.SendMessage(hWnd, 273, wParam, IntPtr.Zero)
        End Sub

        Private Enum Command As Integer
            'Lists the wParams sent to WMP when certain actions are taken
            PlayPause = 18808 '0x00004978
            'Stop is a reserved keyword, so we need to use StopPlayback instead
            StopPlayback = 18809 '0x00004979
            NextTrack = 18811 '0x0000497B
            PrevTrack = 18810 '0x0000497A
            VolumeUp = 18815
            VolumeDown = 18816
            ToggleMute = 18817
            'FastForward = 18813 'Needs further work, this is the message sent by WMP to itself to set play speed to fast, but it doesn't like it from here....
            Repeat = 18843
            Shuffle = 18842
        End Enum

#End Region

        Public Sub New()
            Dim hWnd As IntPtr = NativeMethods.FindWindow("WMPlayerApp", "Windows Media Player")
            If hWnd.ToInt32 = 0 Then
                Throw New PlayerNotRunningException
            End If
        End Sub

        Public Overrides ReadOnly Property SupportedActions() As Collections.ObjectModel.ReadOnlyCollection(Of Action)
            Get
                Dim list As System.Collections.Generic.List(Of Action) = New Collections.Generic.List(Of Action)
                list.Add(Action.PlayPause)
                list.Add(Action.StopPlayback)
                list.Add(Action.NextTrack)
                list.Add(Action.PreviousTrack)
                list.Add(Action.VolumeUp)
                list.Add(Action.VolumeDown)
                list.Add(Action.ToggleMute)
                list.Add(Action.ToggleRepeat)
                list.Add(Action.ToggleShuffle)
                Return New Collections.ObjectModel.ReadOnlyCollection(Of Action)(list)
            End Get
        End Property

        Public Overrides ReadOnly Property Version() As String
            Get
                Return "Windows Media Player Controller v0.2"
            End Get
        End Property

#Region " Playback "

        Public Overrides Sub Play()
            SendCommand(Command.PlayPause)
        End Sub

        Public Overrides Sub PlayPause()
            SendCommand(Command.PlayPause)
        End Sub

        Public Overrides Sub StopPlayback()
            SendCommand(Command.StopPlayback)
        End Sub

        Public Overrides Sub NextTrack()
            SendCommand(Command.NextTrack)
        End Sub

        Public Overrides Sub PreviousTrack()
            SendCommand(Command.PrevTrack)
        End Sub

        Public Overrides Sub VolumeUp()
            SendCommand(Command.VolumeUp)
        End Sub

        Public Overrides Sub VolumeDown()
            SendCommand(Command.VolumeDown)
        End Sub

        Public Overrides Sub ToggleMute()
            SendCommand(Command.ToggleMute)
        End Sub

        Public Overrides Sub ToggleRepeat()
            SendCommand(Command.Repeat)
        End Sub

        Public Overrides Sub ToggleShuffle()
            SendCommand(Command.Shuffle)
        End Sub

#End Region

    End Class
End Namespace
